package com.team.controller;

import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.team.entity.AppInfo;
import com.team.entity.AppVersion;
import com.team.entity.DevUser;
import com.team.service.AppInfoService;
import com.team.service.AppVersionService;
import com.team.utils.AppInfoCondition;
import com.team.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;


@Controller
public class AddBasicAPPInformationController {

    @Autowired(required = false)
    private AppInfoService appInfoService;
    @Autowired
    private AppVersionService appVersionService;
    @RequestMapping(value = "/GetAPPInformation")
    public String GetAPPInformation(HttpSession httpSession, AppInfoCondition appInfoCondition, Model model){

        DevUser devUser = (DevUser)httpSession.getAttribute("devUser");

        appInfoCondition.setDevid(devUser.getId());

        PageInfo<AppInfo> pageInfo = appInfoService.getAllBasicAPPInformation(appInfoCondition);

        model.addAttribute("appInfoList",pageInfo);

        model.addAttribute("condition",appInfoCondition);
        return "jsp/developer/appinfolist";
    }

    @RequestMapping(value = "addappInfo")
    public String addappInfo(AppInfo appInfo,
                             @RequestParam(value = "a_logoPicPath",required = false)CommonsMultipartFile file,
                             HttpServletRequest request, HttpSession httpSession, Model model){
        try {
            //获取保存的根路径
            String realPath = request.getServletContext().getRealPath("jsp/upload/logo/");
            //使用工具类实现上传，返回一个文件名
            String filename = FileUploadUtil.uploadFile(realPath, file);

            appInfo.setLogopicpath("/jsp/upload/logo/"+filename);
            //realPath+"/"+filename在数据库中/会变成\
            appInfo.setLogolocpath(realPath+"\\"+filename);
            DevUser devUser = (DevUser)httpSession.getAttribute("devUser");
            appInfo.setDevid(devUser.getId());
            appInfoService.addAppInfo(appInfo);
            return "redirect:GetAPPInformation";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "jsp/developer/appinfoadd";

    }

    @RequestMapping(value = "/getSingleAppInfo")
    public String getSingleAppInfo(Long id,Model model){
        AppInfo appInfo = appInfoService.getSingleAppInfo(id);

        model.addAttribute("appInfo",appInfo);
        return "jsp/developer/appinfomodify";
    }

    @RequestMapping(value = "/updateAppInfo")
    public String updateAppInfo(AppInfo appInfo,
                                @RequestParam(value = "a_logoPicPath",required = false)CommonsMultipartFile file,
                                HttpServletRequest request,String logolocpath){

        if (!file.isEmpty()){

            String realPath = request.getServletContext().getRealPath("jsp/upload/logo");

            String uploadFile = FileUploadUtil.uploadFile(realPath, file);

            appInfo.setLogopicpath("/jsp/upload/logo/"+uploadFile);

            appInfo.setLogolocpath(realPath+"\\"+uploadFile);

            int i = -1;
                  i = appInfoService.updateAppInfo(appInfo);
            if (i > 0){
                //删除旧文件
                new File(logolocpath).delete();
            }else {
                new File(realPath+"\\"+uploadFile).delete();
            }

        }
        return "redirect:GetAPPInformation";

    }

   //同步请求删除
    @RequestMapping("/delAppInfo")
    public String delAppInfo(Long id,String logoLocPath){
        try {
            //删除对应的图片

            File file = new File(logoLocPath);

            if (file.exists())
                file.delete();
            //删除app版本的对应文件
            appInfoService.delAppInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:GetAPPInformation";
    }

    //异步请求删除

    @RequestMapping("/deleteAppInfo")
    public String deleteAppInfo(Long id,String logoLocPath){
        try {
            //删除对应的图片

            File file = new File(logoLocPath);

            //删除app版本的对应文件
            appInfoService.delAppInfo(id);

            if (file.exists())
                file.delete();

            return "{\"result\":1}";  //1表示成功
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "{\"result\":0}";  //0表示删除失败
    }

    @RequestMapping(value = "getAppVersion")
    public String getAppVersion(Long id,Model model){
        List<AppVersion> list = appVersionService.getAppVersion(id);
        model.addAttribute("appVersions",list);
        model.addAttribute("appid",id);
        return "jsp/developer/appversionadd";

    }

    @RequestMapping(value = "getAppInfoByStatus")
    public String getAppInfoByStatus(AppInfoCondition appInfoCondition,Model model){
        PageInfo<AppInfo> appInfoByStatus = appInfoService.getAppInfoByStatus(appInfoCondition);
        model.addAttribute("appInfoList",appInfoByStatus);
        model.addAttribute("condition",appInfoCondition);
        return "jsp/backend/applist";
    }



}
